package com.weiyun.service.impl;

import com.weiyun.mapper.BusinessMapper;
import com.weiyun.mapper.CartMapper;
import com.weiyun.mapper.FoodMapper;
import com.weiyun.pojo.Business;
import com.weiyun.pojo.Cart;
import com.weiyun.pojo.Food;
import com.weiyun.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/***
 *   @Author:weiyun
 *
 */
@Service
public class CartServiceImpl  implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public List<Cart> listCart(Cart cart) throws Exception {
        List<Cart> cartList = cartMapper.listCart(cart);
        for(Cart c : cartList){
            Food foodById = foodMapper.getFoodById(c.getFoodId());
            if(foodById!=null){
                c.setFood(foodById);
            }
            Business business = businessMapper.getBusinessById(cart.getBusinessId());
            if(business!=null){
                c.setBusiness(business);
            }
        }
        return cartList;
    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        cart.setQuantity(1);
        return cartMapper.saveCart(cart);
    }

    @Override
    public int removeCart(Cart cart) throws Exception {
        return cartMapper.removeCart(cart);
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        return cartMapper.updateCart(cart);
    }
}
